﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using SD.Database.Classes;
using System.Collections.Generic;
using DBAccess;
namespace SD.Database
{

    public partial class StoredProcedures
    {
        [Microsoft.SqlServer.Server.SqlProcedure]
        public static void UpdateCulture()
        {
            int intervalTime = int.Parse(Configuration.GetConfigurationForOption("SmallUpdateInterval"));//in seconds
            double CultureBonus = double.Parse(Configuration.GetConfigurationForOption("CultureBonus"));
            DataSet ds = DAL.GetInstance().GetData(@"SELECT     SUM(Planets.Cybernets) AS Expr1, Players.PlayerId, Players.Culture, CurrentCulture, ArtifactID
FROM         Planets INNER JOIN Players ON Planets.OwnerID = Players.PlayerId
GROUP BY Players.PlayerId, Players.Culture, CurrentCulture, ArtifactID
HAVING      (Players.PlayerId > 1)");

            if (ds.Tables.Count == 0) return;

            Dictionary<string, double> commands = new Dictionary<string, double>();
            foreach (DataRow rdr in ds.Tables[0].Rows)
            {
                int culture = int.Parse(rdr["Culture"].ToString());
                int pId = int.Parse(rdr["PlayerID"].ToString());
                double sciH = (int)double.Parse(rdr["Expr1"].ToString());
                double cLv = double.Parse(rdr["CurrentCulture"].ToString());
                int levelPoints = Equations.CultureCost((int)cLv);
                //get bonus from race and artifact
                double bonus = (1 + culture * CultureBonus);
                string t = rdr["ArtifactID"] as string;
                if (t != null)
                {
                    switch (t)
                    {
                        case "GT": bonus += 0.1; break;
                        case "ITV": bonus += 0.2; break;
                        case "BHS": bonus -= 0.1; break;
                        case "STr": bonus += 0.1; break;
                        case "AF": bonus -= 0.3; break;
                        case "MOT": bonus += 0.2; break;
                    }
                }
                sciH = sciH * bonus;
                if (sciH < 0)
                    sciH = 0.01;
                //get percentage
                double currentValue = (1.0 - (cLv - (int)cLv)) * levelPoints * 1.0;
                double newValue = currentValue * 1.0 - sciH / (double)(3600.0 / intervalTime);
                double valueToStore;
                if (newValue <= 0) //new level
                {
                    valueToStore = 1.0 - (Equations.CultureCost((int)cLv + 1) + newValue) * 1.0 / Equations.CultureCost((int)cLv + 1) + (int)cLv + 1;
                }
                else
                {
                    valueToStore = 1.0 - (newValue) / levelPoints + 1.0 * (int)cLv;
                }
                //store in the db
                DAL.GetInstance().UpdateData("Update Players Set CurrentCulture = @param0 Where PlayerID = @param1", new List<object>() { valueToStore, pId });
            }
        }
    };
}